package com.intellagric.controller.page;

import com.fasterxml.jackson.core.JsonParseException;
import com.intellagric.common.utils.CookieUtils;
import com.intellagric.common.utils.JsonUtils;
import com.intellagric.interfaces.login.LoginService;
import com.intellagric.interfaces.sys_user.UserService;
import com.intellagric.pojo.SysUser;
import com.intellagric.service.login.LoginServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2019/1/22
 * @DESC： 权限过滤
 */

public class AdminFilter implements Filter {

    @Autowired
    private UserService userService;

    @Value("${TOKEN_KEY}")
    private String TOKEN_KEY;
    @Value("${SESSION_EXPIRE}")
    private Integer SESSION_EXPIRE;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //获取cookie
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

        String user = CookieUtils.getCookieValue(httpServletRequest, TOKEN_KEY, false);




        if (StringUtils.isBlank(user)) {
            //用户登录已过期
            httpServletResponse.sendRedirect("/login");
        } else {
            //拿到客户端ip，看是否跟cookie里存储的ip一致
            LoginService loginServiceImpl=new LoginServiceImpl();
            String ip = loginServiceImpl.getClientIp(httpServletRequest);
//            System.out.println("ip=========="+ip);

            try {
                SysUser sysUser = JsonUtils.jsonToPojo(user, SysUser.class);
                if (sysUser != null) {
                    if (sysUser.getLoginIp().equals(ip)) {
                        //先去数据库查看是否真的有这个用户

                        SysUser realUser= userService.getUserById(sysUser.getId());
                        if(realUser!=null) {
                            //每次操作都会更新cookie的保存时间

                            CookieUtils.setCookie(httpServletRequest,httpServletResponse,TOKEN_KEY,user,SESSION_EXPIRE);

                            filterChain.doFilter(servletRequest, servletResponse);
                        }else
                            httpServletResponse.sendRedirect("/login");

                    } else
                        httpServletResponse.sendRedirect("/login");
                } else
                    httpServletResponse.sendRedirect("/login");
            }catch (JsonParseException e){
                //user字符串（防止恶意修改taken的格式导致的报错）不能转化为user对象
                httpServletResponse.sendRedirect("/login");
            }
        }

    }

    @Override
    public void destroy() {

    }
}
